// pages/merchant/index/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl:app.globalData.imgUrl,
    productTypeList:[],
    productList:[],
    productId:'',
    productTypeId:'',
    deptId:'',
    deptList:[],
    goodsList:[],
    totalPage: 0, // 总页数
    queryParams: {
      page: 1,
      size: 10,
    },
    pullFlag: false, // 是否向上拉动到底部
    listTip: '上拉加载更多'
  },
  onshow(){
    this.getList()
  },
  // 商品类型修改
  onChange(e){
    let id=e.currentTarget.dataset.id
    let product=[]
    this.data.productTypeList.forEach(element => {
      if(element.id==id){
        product=element.children
      }
    });
    this.setData({productTypeId:id,productList:product,productId:''})
    this.getList()
},
// 商品修改
  onChange1(e){
      let id=e.currentTarget.dataset.id
      this.setData({productId:id})
      this.getList()
  },
  // 行政区修改
  onChange2(e){
    let id=e.currentTarget.dataset.id
    this.setData({deptId:id})
    this.getList()
},
  onReady(){
    // 获取商品列表
    wx.$req({
      url: '/wms/itemType/treeselect',
    }).then(res => {
      let list=res.data.data
      let type=''
      let product=[]
      if(list.length>0){
        list.forEach(element => {
          element.children.unshift({id:'',label:'全部'})
        });
        // type=list.id
        // product=list.children
        // product.unshift({id:'',label:'全部'})
      }
      list.unshift({id:'',label:'全部',children:[{id:'',label:'全部'}]})
      product=list[0].children
      this.setData({ 
        productTypeList: list,
        productTypeId:type,
        productList:product
      })
    })

    // 获取行政区列表
    wx.$req({
      url: '/wms/orderPurchase/getDept',
    }).then(res => {
      let depts=[]
      if(res.data.length>0){
        res.data.forEach(element => {
          depts.push({deptId:element.deptId,deptName:element.deptName})
        });
      }
      depts.unshift({deptId:'',deptName:'全部'})
      this.setData({ 
        deptList:depts
      })
    })

    this.getList()
  },
  // 获取商品
  getList(){
    this.setData({ listTip: '加载中...' })
    const { page, size } = this.data.queryParams
    wx.$req({
      url: `/wms/orderPurchase/receiveList?page=${page - 1}&size=${size}`,
      method: 'post',
      data:{productId:this.data.productId,typeId:this.data.productTypeId,deptId:this.data.deptId},
      loadingText: '加载中',
    }).then(res => {
      let totalPage = res.data.totalPages
      let rows = this.data.goodsList

      if (totalPage == 1 || this.data.totalPage === totalPage ) {
        this.setData({ listTip: '暂无更多' })
      } else {
        this.setData({ listTip: '上拉加载更多' })
      }

      if (totalPage == 0) {
        this.setData({ listTip: '暂无内容' })
      }
    
      // 列表数据：为true 则需要push, false，则直接覆盖
      if (this.data.pullFlag) {
        rows.push(...res.data.content)
      } else {
        rows = res.data.content
      }

      this.setData({ 
        goodsList: rows,
        totalPage
      })
    }).finally(() => { wx.stopPullDownRefresh() })
  },
/**
	* 页面相关事件处理函数--监听用户下拉动作
	*/
	onPullDownRefresh() {
		const queryParams = { page: 1,  size: 10 }

		this.setData({ 
			pullFlag: false,
			queryParams
		})

		this.getList()
	},
	
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

    let { page, size } = this.data.queryParams

    if (page < this.data.totalPage) {

      page += 1
      this.setData({  
        pullFlag: true,
        queryParams: { page,  size },
      })
      this.getList()
    }
  },
// 跳转商品列表
  toGoods () {
    wx.navigateTo({
      url: '/pageMerchant/goodsList/index',
    })
  },

  toPurchase() {
    wx.navigateTo({
      url: '/pageMerchant/myPurchase/index',
    })
  },
  // 获取商品详情页
  toInfo (e) {
    const info = JSON.stringify(e.currentTarget.dataset.item)
    wx.navigateTo({
      url: "/pageMerchant/goodsInfo/index?info="+info
    })
  }

})
